﻿using System.Collections.Generic;

using DanielVaughan;
using DanielVaughan.ComponentModel;
using DanielVaughan.Messaging;
using DanielVaughan.Services;
using DanielVaughan.UserOptionsModel;
using DanielVaughan.UserOptionsModel.UserOptions;

namespace CalciumTemplateApp
{
	/// <summary>
	/// This class contains strongly typed setting properties, 
	/// and is used to initialize the user options that are displayed on the app's 
	/// options view.
	/// Add your own options to the options collection in the InitializeUserOptions method.
	/// </summary>
	public class AppSettings : ObservableObject, IMessageSubscriber<SettingChangeEventArgs>
	{
		readonly ISettingsService settingsService = Dependency.Resolve<ISettingsService>();
		static bool userOptionsInitialized;

		public static void InitializeUserOptions()
		{
			if (userOptionsInitialized)
			{
				return;
			}
			userOptionsInitialized = true;

			var userOptionsService = Dependency.Resolve<IUserOptionsService>();
			OptionCategory defaultCategory = new OptionCategory(
							"General", () => MainResources.OptionCategories_General);
		
			/* Add options to the following collection and they will be 
			 * automatically displayed on the options page. 
			 * Each UserOption object must have a unique key. 
			 * Lambda expression are used for most of the properties 
			 * to allow your app to switch languages without requiring a restart. 
			 * You can specify a template for the option by setting its TemplateFunc property.
			 * See the UserOptionBase implementations. */
			List<IUserOption> options = new List<IUserOption>
			        {
						new BooleanUserOption(() => MainResources.Options_SystemTrayVisible_Title, systemTrayVisibleKey, () => true),
						/* *** Add options here *** */
						//new StringUserOption(() => "Name", "ExampleString1", () => "Katka", "Url"),
			        };
			
			userOptionsService.Register(options, defaultCategory);
		}

		const string systemTrayVisibleKey = "SystemTrayVisible";

		public bool SystemTrayVisible
		{
			get
			{
				return settingsService.GetSetting(systemTrayVisibleKey, true);
			}
			set
			{
				settingsService.SetSetting(systemTrayVisibleKey, value);
			}
		}

		/* Use the following template for new settings. */
		//	const string mySettingKey = "MySetting";
		//
		//	public string MySetting
		//	{
		//		get
		//		{
		//			return settingsService.GetSetting(mySettingKey, null);
		//		}
		//		set
		//		{
		//			settingsService.SetSetting(mySettingKey, value);
		//		}
		//	}

		#region Singleton implementation

		static readonly AppSettings instance = new AppSettings();

		AppSettings()
		{
			var messageBus = Dependency.Resolve<IMessageBus>();
			messageBus.Subscribe(this);
		}

		public static AppSettings Instance
		{
			get
			{
				return instance;
			}
		}

		#endregion

		/// <summary>
		/// Receives the settings changed message from the message bus.
		/// We signal that the setting has changed by using the convention 
		/// that the property name is the setting name.
		/// </summary>
		/// <param name="message">The <see cref="DanielVaughan.Services.SettingChangeEventArgs"/> 
		/// instance containing the event data.</param>
		public void ReceiveMessage(SettingChangeEventArgs message)
		{
			OnPropertyChanged(message.SettingName);
		}
	}
}
